import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexWriter;

public class DemoMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			IndexWriter writer = new IndexWriter("myindex",
					new StandardAnalyzer(), true);

			File files = new File("mydoc");
			String[] Fnamelist = files.list();
			for (int i = 0; i < Fnamelist.length; i++) {
				File file = new File(files, Fnamelist[i]);

				Document doc = new Document();
				Field fld = new Field("path", file.getPath());
				doc.add(fld);

				fld = Field.Keyword("modified",
						DateField.timeToString(file.lastModified()));
				doc.add(fld);

				FileInputStream in = new FileInputStream(file);
				Reader reader = new BufferedReader(new InputStreamReader(in));
				fld = Field.Text("contents", reader);
				Field f=new Field("all content", reader,Field.Store.YES);
				
				doc.add(fld);

				writer.addDocument(doc);
				System.out.println("Added : " + doc.get("path"));

			}
			writer.optimize();
			writer.close();
			System.out.println("Has Added Total: " + Fnamelist.length);
		} catch (Exception e) {
			System.out.println(e);
		}

	}

}
